The Bucket Pattern is used when you need to manage large volumes of time-series data, IoT sensor readings, or event logs by grouping related documents into fixed-size buckets to reduce document count and improve query performance.
The Bucket Pattern is a powerful MongoDB schema design pattern that addresses the challenge of storing large volumes of sequential data. Instead of creating one document per data point, you group multiple related data points into a single document (a "bucket") based on a logical boundary like time or a fixed count of items. This pattern is particularly effective for time-series data, IoT sensor readings, financial tick data, and event logs where data arrives continuously and is typically queried in ranges .
Time-series data: When you need to store sensor readings, stock ticks, or application metrics that arrive at regular intervals. The pattern reduces millions of documents to thousands .
High-volume event logging: For application logs or user activity streams where queries typically ask for data over time ranges (e.g., "show me all errors in the last hour") .
Data that is rarely updated individually: The bucket pattern works best when individual data points are inserted but not modified. Pre-aggregated values like averages can be updated in the bucket document .
Range-based queries: When you frequently query data by time ranges (hourly, daily), the bucket pattern allows efficient retrieval of a whole time range by scanning fewer documents .
You need to reduce index size: With one document per data point, indexes become enormous. Bucketing dramatically reduces the number of documents and index entries .
The Bucket Pattern delivers significant performance improvements by reducing the total document count, which directly reduces the size of indexes and the number of disk seeks required for queries . With pre-aggregated values (like min, max, and average), many queries can be answered without accessing individual readings at all . Real-world case studies show that this pattern can reduce storage by 60% and improve query performance by orders of magnitude .
Bucket size: Choose a bucket size that balances document growth against query granularity. Hourly buckets with 60 one-minute readings are common, but daily buckets with 1440 minute-level readings might be too large .
Document growth: Ensure buckets don't grow indefinitely. Use fixed-size buckets based on time windows (hourly, daily) or maximum element counts .
Pre-aggregation: Store computed values like counts, sums, and averages to answer common queries without scanning individual readings .
Write patterns: The bucket pattern handles high-frequency writes efficiently because multiple writes update the same document rather than creating new ones .